﻿using System;
using System.Collections.Generic;
using System.Linq;
using QuanLyTiemThuocTay.cls;

namespace QuanLyTiemThuocTay.Report
{
    public partial class rptPhieuXuat : DevExpress.XtraReports.UI.XtraReport
    {
        QLTTDataContext db = new QLTTDataContext();
        List<CHITIETXUATDVT> lctxdvt = new List<CHITIETXUATDVT>();
        HDXUAT hdx;
        private int STT;
        int tongsoluong = 0;
        int tongtien = 0;
        public rptPhieuXuat(string sophieu)
        {
            InitializeComponent();
            hdx = db.HDXUATs.SingleOrDefault(x => x.SoHDXuat == sophieu);
            if (hdx!=null)
            {
                List<CHITIETXUAT> lctn = db.CHITIETXUATs.Where(x => x.SoHDXuat == sophieu).ToList();
                if (hdx.BanLe)
                    DisplayName = "Hóa đơn bán lẻ";
                else
                    DisplayName = "Hóa đơn bán buôn";
                xrName.Text = DisplayName.ToUpper();
                lbSoPhieu.Text = sophieu;
                var kh = db.KHACHHANGs.SingleOrDefault(x => x.TenKH == hdx.XuatChoDonVi);
                lbNgay.Text = DateTime.Parse(hdx.NgayXuat.ToString()).Day + "/" + DateTime.Parse(hdx.NgayXuat.ToString()).Month + "/" + DateTime.Parse(hdx.NgayXuat.ToString()).Year;
                lbNhaCC.Text = hdx.XuatChoDonVi;
                lbDiaChi.Text = kh != null ? kh.DiaChi : "";
                if (hdx != null)
                {
                    foreach (var item in lctn)
                    {
                        CHITIETXUATDVT te = new CHITIETXUATDVT();
                        te.MaChiTiet = item.MaChiTiet;
                        te.MaDonGia = item.MaDonGia;
                        te.DonViTinh = item.GIATHUOC.THUOC.DVTINH.TenDVTinh; ;
                        te.MaThuoc = item.MaThuoc;
                        te.SoHDXuat = sophieu;
                        te.SoLuong = item.SoLuong;
                        lctxdvt.Add(te);
                    }
                    try
                    {
                        DataSource = lctxdvt;
                    }
                    catch { }
                    STT = 0;
                }
            }
        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            STT++;
            xrTT.Text = STT.ToString();
            try { xrTen.Text = "  " + db.THUOCs.SingleOrDefault(x => x.MaThuoc == lctxdvt[STT - 1].MaThuoc).TenThuoc; }
            catch { }
            try { xrDVT.Text = "  " + lctxdvt[STT - 1].DonViTinh; }
            catch { }
            try
            {
                if (hdx.BanLe)
                    xrDonGia.Text = db.GIATHUOCs.SingleOrDefault(x => x.MaDonGia == lctxdvt[STT - 1].MaDonGia).GiaBanLe + "";
                else
                    xrDonGia.Text = db.GIATHUOCs.SingleOrDefault(x => x.MaDonGia == lctxdvt[STT - 1].MaDonGia).GiaBanBuon + "";
            }
            catch { }
            try
            {
                xrSoLuong.Text = lctxdvt[STT - 1].SoLuong + "";
                tongsoluong += (int)lctxdvt[STT - 1].SoLuong;
            }
            catch { }
            try
            {
                xrThanhTien.Text = (lctxdvt[STT - 1].SoLuong * Int32.Parse(xrDonGia.Text)) + "";
                tongtien += Int32.Parse(xrThanhTien.Text);
            }
            catch { }
            xrTongSLuong.Text = tongsoluong + "";
            xrTongTien.Text = tongtien + " vnđ";
            xrSoTienChu.Text = "Tổng tiền (bằng chữ): "+clsHam.DocTienBangChu(tongtien, " đồng y.");
        }

    }
}
